/**
 * 应急知识手册服务层
 */

// 文档分类
export const documentCategories = [
  { id: 'all', name: '全部', icon: 'el-icon-document' },
  { id: 'epidemic', name: '疫情防控', icon: 'el-icon-warning' },
  { id: 'rescue', name: '应急救援', icon: 'el-icon-first-aid-kit' },
  { id: 'management', name: '应急管理', icon: 'el-icon-s-management' },
  { id: 'law', name: '法律法规', icon: 'el-icon-document-checked' },
  { id: 'guideline', name: '操作指南', icon: 'el-icon-notebook-2' }
];

// 模拟文档数据
const mockDocuments = [
  {
    id: 'DOC-2025-001',
    title: '突发公共卫生事件应急条例',
    category: 'law',
    categoryName: '法律法规',
    description: '国务院发布的突发公共卫生事件应急处理的综合性法规文件，规范应急处置工作的法律依据。',
    fileType: 'pdf',
    fileSize: '2.5 MB',
    uploadDate: '2025-01-15',
    downloadCount: 1256,
    author: '国务院办公厅',
    tags: ['法规', '应急条例', '公共卫生'],
    status: '有效',
    updateDate: '2025-01-15'
  },
  {
    id: 'DOC-2025-002',
    title: '新冠病毒感染防控方案（第十版）',
    category: 'epidemic',
    categoryName: '疫情防控',
    description: '最新版本的新冠病毒感染防控方案，包含疫情监测、流调、隔离、消毒等全流程防控措施。',
    fileType: 'pdf',
    fileSize: '3.8 MB',
    uploadDate: '2025-02-20',
    downloadCount: 2345,
    author: '国家疾控局',
    tags: ['新冠', '防控方案', '疫情'],
    status: '有效',
    updateDate: '2025-02-20'
  },
  {
    id: 'DOC-2025-003',
    title: '流行病学调查工作指南',
    category: 'guideline',
    categoryName: '操作指南',
    description: '详细介绍流行病学调查的标准流程、方法、注意事项，包括现场调查、数据采集、分析报告等内容。',
    fileType: 'pdf',
    fileSize: '4.2 MB',
    uploadDate: '2025-01-10',
    downloadCount: 1890,
    author: '中国疾控中心',
    tags: ['流调', '操作指南', '现场调查'],
    status: '有效',
    updateDate: '2025-01-10'
  },
  {
    id: 'DOC-2025-004',
    title: '卫生应急队伍装备配置规范',
    category: 'rescue',
    categoryName: '应急救援',
    description: '规定各类卫生应急队伍的装备配置标准，包括个人防护装备、医疗设备、通讯设备等。',
    fileType: 'doc',
    fileSize: '1.8 MB',
    uploadDate: '2024-12-05',
    downloadCount: 876,
    author: '国家卫健委',
    tags: ['装备', '应急队伍', '配置标准'],
    status: '有效',
    updateDate: '2024-12-05'
  },
  {
    id: 'DOC-2025-005',
    title: '突发事件应急预案管理办法',
    category: 'management',
    categoryName: '应急管理',
    description: '规范应急预案的编制、审批、发布、演练、修订等全流程管理工作，提高应急预案的科学性和有效性。',
    fileType: 'pdf',
    fileSize: '2.1 MB',
    uploadDate: '2024-11-20',
    downloadCount: 1543,
    author: '应急管理部',
    tags: ['应急预案', '管理办法', '预案编制'],
    status: '有效',
    updateDate: '2024-11-20'
  },
  {
    id: 'DOC-2025-006',
    title: '传染病疫情报告管理规范',
    category: 'management',
    categoryName: '应急管理',
    description: '明确传染病疫情信息报告的时限、内容、程序和责任，确保疫情信息及时准确报告。',
    fileType: 'pdf',
    fileSize: '1.5 MB',
    uploadDate: '2024-10-15',
    downloadCount: 2103,
    author: '国家卫健委',
    tags: ['疫情报告', '传染病', '管理规范'],
    status: '有效',
    updateDate: '2024-10-15'
  },
  {
    id: 'DOC-2025-007',
    title: '现场消杀技术指南',
    category: 'guideline',
    categoryName: '操作指南',
    description: '针对不同场所和污染类型的消毒杀菌技术指南，包括消毒剂选择、配比方法、操作流程等。',
    fileType: 'pdf',
    fileSize: '3.2 MB',
    uploadDate: '2024-09-28',
    downloadCount: 1687,
    author: '中国疾控中心',
    tags: ['消毒', '现场处置', '技术指南'],
    status: '有效',
    updateDate: '2024-09-28'
  },
  {
    id: 'DOC-2025-008',
    title: '食品安全事故应急处置预案',
    category: 'rescue',
    categoryName: '应急救援',
    description: '针对食品安全突发事件的应急处置预案，包括事故分级、响应机制、处置措施等内容。',
    fileType: 'pdf',
    fileSize: '2.8 MB',
    uploadDate: '2024-08-12',
    downloadCount: 945,
    author: '国家市场监管总局',
    tags: ['食品安全', '应急处置', '预案'],
    status: '有效',
    updateDate: '2024-08-12'
  },
  {
    id: 'DOC-2025-009',
    title: '个人防护装备使用规范',
    category: 'guideline',
    categoryName: '操作指南',
    description: '详细说明各类个人防护装备（口罩、防护服、护目镜等）的正确穿戴方法和使用注意事项。',
    fileType: 'pdf',
    fileSize: '5.1 MB',
    uploadDate: '2024-07-18',
    downloadCount: 3120,
    author: '中国疾控中心',
    tags: ['防护装备', '使用规范', '个人防护'],
    status: '有效',
    updateDate: '2024-07-18'
  },
  {
    id: 'DOC-2025-010',
    title: '职业暴露后处置流程',
    category: 'rescue',
    categoryName: '应急救援',
    description: '医务人员发生职业暴露后的紧急处置流程，包括伤口处理、报告程序、预防性用药等。',
    fileType: 'doc',
    fileSize: '1.2 MB',
    uploadDate: '2024-06-30',
    downloadCount: 1234,
    author: '国家卫健委',
    tags: ['职业暴露', '应急处置', '医务人员'],
    status: '有效',
    updateDate: '2024-06-30'
  },
  {
    id: 'DOC-2025-011',
    title: '突发环境污染事件卫生应急技术方案',
    category: 'rescue',
    categoryName: '应急救援',
    description: '针对化学品泄漏、水源污染等环境突发事件的卫生应急技术方案和处置措施。',
    fileType: 'pdf',
    fileSize: '4.5 MB',
    uploadDate: '2024-05-22',
    downloadCount: 768,
    author: '生态环境部',
    tags: ['环境污染', '应急技术', '卫生防护'],
    status: '有效',
    updateDate: '2024-05-22'
  },
  {
    id: 'DOC-2025-012',
    title: '应急演练组织实施指南',
    category: 'management',
    categoryName: '应急管理',
    description: '指导各单位科学组织开展应急演练活动，包括演练方案设计、演练实施、演练评估等。',
    fileType: 'pdf',
    fileSize: '2.3 MB',
    uploadDate: '2024-04-10',
    downloadCount: 1456,
    author: '应急管理部',
    tags: ['应急演练', '组织实施', '演练评估'],
    status: '有效',
    updateDate: '2024-04-10'
  },
  {
    id: 'DOC-2025-013',
    title: '疫苗接种异常反应处置规范',
    category: 'epidemic',
    categoryName: '疫情防控',
    description: '规范疫苗接种异常反应的监测、报告、调查、诊断和处置工作，保障接种安全。',
    fileType: 'pdf',
    fileSize: '2.9 MB',
    uploadDate: '2024-03-15',
    downloadCount: 2087,
    author: '国家疾控局',
    tags: ['疫苗接种', '异常反应', '处置规范'],
    status: '有效',
    updateDate: '2024-03-15'
  },
  {
    id: 'DOC-2025-014',
    title: '突发中毒事件应急处置手册',
    category: 'rescue',
    categoryName: '应急救援',
    description: '包含常见中毒事件的识别、现场急救、转运救治等应急处置措施和技术要点。',
    fileType: 'pdf',
    fileSize: '3.6 MB',
    uploadDate: '2024-02-28',
    downloadCount: 1345,
    author: '中国疾控中心',
    tags: ['中毒事件', '应急处置', '急救手册'],
    status: '有效',
    updateDate: '2024-02-28'
  },
  {
    id: 'DOC-2025-015',
    title: '传染病防控法律法规汇编',
    category: 'law',
    categoryName: '法律法规',
    description: '汇集传染病防治相关的法律、法规、规章和规范性文件，便于应急工作人员查阅使用。',
    fileType: 'pdf',
    fileSize: '6.8 MB',
    uploadDate: '2024-01-20',
    downloadCount: 1789,
    author: '国家卫健委',
    tags: ['法律法规', '传染病', '防控汇编'],
    status: '有效',
    updateDate: '2024-01-20'
  },
  {
    id: 'DOC-2024-016',
    title: '核辐射事故医学应急处置规范',
    category: 'rescue',
    categoryName: '应急救援',
    description: '针对核辐射事故的医学应急准备和处置规范，包括辐射损伤救治、人员防护等内容。',
    fileType: 'pdf',
    fileSize: '4.1 MB',
    uploadDate: '2023-12-08',
    downloadCount: 542,
    author: '国家核安全局',
    tags: ['核辐射', '医学应急', '辐射防护'],
    status: '有效',
    updateDate: '2023-12-08'
  },
  {
    id: 'DOC-2024-017',
    title: '自然灾害卫生应急工作指南',
    category: 'rescue',
    categoryName: '应急救援',
    description: '地震、洪涝、台风等自然灾害后的卫生应急工作指南，包括防疫、救治、心理干预等。',
    fileType: 'pdf',
    fileSize: '5.3 MB',
    uploadDate: '2023-11-15',
    downloadCount: 1023,
    author: '国家减灾中心',
    tags: ['自然灾害', '卫生应急', '灾后防疫'],
    status: '有效',
    updateDate: '2023-11-15'
  },
  {
    id: 'DOC-2024-018',
    title: '应急物资储备管理办法',
    category: 'management',
    categoryName: '应急管理',
    description: '规范应急物资的储备、管理、调配和使用，确保应急物资储备充足、管理规范。',
    fileType: 'doc',
    fileSize: '1.9 MB',
    uploadDate: '2023-10-20',
    downloadCount: 1567,
    author: '应急管理部',
    tags: ['应急物资', '储备管理', '物资调配'],
    status: '有效',
    updateDate: '2023-10-20'
  }
];

// 统计数据
const mockStatistics = {
  totalDocuments: mockDocuments.length,
  totalDownloads: mockDocuments.reduce((sum, doc) => sum + doc.downloadCount, 0),
  categoryCount: documentCategories.length - 1, // 不计算"全部"分类
  newThisMonth: 5
};

/**
 * 获取文档列表
 */
export function getDocumentList(params = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      let results = [...mockDocuments];

      // 按分类筛选
      if (params.category && params.category !== 'all') {
        results = results.filter(doc => doc.category === params.category);
      }

      // 按关键词搜索
      if (params.keyword) {
        const keyword = params.keyword.toLowerCase();
        results = results.filter(doc =>
          doc.title.toLowerCase().includes(keyword) ||
          doc.description.toLowerCase().includes(keyword) ||
          doc.author.toLowerCase().includes(keyword) ||
          doc.tags.some(tag => tag.toLowerCase().includes(keyword))
        );
      }

      // 按文件类型筛选
      if (params.fileType) {
        results = results.filter(doc => doc.fileType === params.fileType);
      }

      // 按标签筛选
      if (params.tag) {
        results = results.filter(doc => doc.tags.includes(params.tag));
      }

      // 排序
      if (params.sortBy) {
        switch (params.sortBy) {
          case 'date-desc':
            results.sort((a, b) => new Date(b.uploadDate) - new Date(a.uploadDate));
            break;
          case 'date-asc':
            results.sort((a, b) => new Date(a.uploadDate) - new Date(b.uploadDate));
            break;
          case 'download-desc':
            results.sort((a, b) => b.downloadCount - a.downloadCount);
            break;
          case 'download-asc':
            results.sort((a, b) => a.downloadCount - b.downloadCount);
            break;
          case 'title':
            results.sort((a, b) => a.title.localeCompare(b.title, 'zh-CN'));
            break;
        }
      } else {
        // 默认按上传日期降序
        results.sort((a, b) => new Date(b.uploadDate) - new Date(a.uploadDate));
      }

      resolve({
        success: true,
        data: results,
        total: results.length
      });
    }, 500);
  });
}

/**
 * 获取文档详情
 */
export function getDocumentDetail(documentId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const document = mockDocuments.find(doc => doc.id === documentId);
      if (document) {
        resolve({
          success: true,
          data: document
        });
      } else {
        reject(new Error('文档不存在'));
      }
    }, 300);
  });
}

/**
 * 下载文档
 */
export function downloadDocument(documentId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const document = mockDocuments.find(doc => doc.id === documentId);
      if (document) {
        // 更新下载次数
        document.downloadCount += 1;
        
        resolve({
          success: true,
          message: '文档下载成功',
          data: {
            documentId: document.id,
            title: document.title,
            downloadUrl: `/downloads/${document.id}.${document.fileType}`,
            downloadCount: document.downloadCount
          }
        });
      } else {
        reject(new Error('文档不存在'));
      }
    }, 800);
  });
}

/**
 * 在线预览文档
 */
export function previewDocument(documentId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const document = mockDocuments.find(doc => doc.id === documentId);
      if (document) {
        resolve({
          success: true,
          message: '文档预览加载成功',
          data: {
            documentId: document.id,
            title: document.title,
            previewUrl: `/preview/${document.id}`,
            fileType: document.fileType
          }
        });
      } else {
        reject(new Error('文档不存在'));
      }
    }, 600);
  });
}

/**
 * 获取统计数据
 */
export function getStatistics() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        data: { ...mockStatistics }
      });
    }, 300);
  });
}

/**
 * 获取热门文档（下载次数最多的前N个）
 */
export function getHotDocuments(limit = 5) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const hotDocs = [...mockDocuments]
        .sort((a, b) => b.downloadCount - a.downloadCount)
        .slice(0, limit);
      
      resolve({
        success: true,
        data: hotDocs
      });
    }, 400);
  });
}

/**
 * 获取最新文档
 */
export function getLatestDocuments(limit = 5) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const latestDocs = [...mockDocuments]
        .sort((a, b) => new Date(b.uploadDate) - new Date(a.uploadDate))
        .slice(0, limit);
      
      resolve({
        success: true,
        data: latestDocs
      });
    }, 400);
  });
}

/**
 * 获取所有标签
 */
export function getAllTags() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const tagsSet = new Set();
      mockDocuments.forEach(doc => {
        doc.tags.forEach(tag => tagsSet.add(tag));
      });
      
      resolve({
        success: true,
        data: Array.from(tagsSet)
      });
    }, 300);
  });
}

/**
 * 按分类统计文档数量
 */
export function getDocumentCountByCategory() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const counts = {};
      documentCategories.forEach(cat => {
        if (cat.id === 'all') {
          counts[cat.id] = mockDocuments.length;
        } else {
          counts[cat.id] = mockDocuments.filter(doc => doc.category === cat.id).length;
        }
      });
      
      resolve({
        success: true,
        data: counts
      });
    }, 300);
  });
}

